package viper.receive;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.SocketException;

import viper.main.StateManager;

public class UDPReceiver implements Runnable {

	public final static int MAX_PACKET_SIZE = 50; 
	
	@Override
	public void run() {
		while(StateManager.INSTANCE.getState()!=StateManager.State.LOGGED_OUT)
		{
			try 
			{
				DatagramPacket packet = 
					new DatagramPacket(new byte[MAX_PACKET_SIZE], MAX_PACKET_SIZE);
				StateManager.INSTANCE.getDatagramSocket().receive(packet);
				//byte[] data = packet.getData();
				StateManager.INSTANCE.getPool().execute(new UDPReceiverWorker(packet));
				System.out.println("got packet");
			} 
			catch (SocketException e) {
				e.printStackTrace();
			} 
			catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
